<template>
  <div style="display: flex; height: 100vh">
    <div style="width: 100%">
      <el-form style="background-color: #fff; height: 176px; padding: 25px">
        <el-row :gutter="24">
          <el-col :span="6"
            ><p>用户名称</p>
            <el-input
              v-model="form.userName"
              placeholder="请输入用户名称"
              style="width: 260px; height: 40px"
          /></el-col>
          <el-col :span="6"
            ><p>手机号</p>
            <el-input
              v-model="form.phone"
              placeholder="请输入手机号码"
              style="width: 260px; height: 40px"
          /></el-col>
          <el-col :span="6">
            <p>状态</p>
            <el-select
              v-model="form.status"
              class="m-2"
              placeholder="用户状态"
              style="width: 260px"
              size="large"
            >
              <el-option label="正常" value="0"></el-option>
              <el-option label="停用" value="1"></el-option>
            </el-select>
          </el-col>
          <el-col :span="6">
            <div class="block">
              <p class="demonstration">创建时间</p>
              <el-date-picker
                style="width: 260px; height: 40px"
                v-model="form.creatTime"
                type="datetimerange"
                :shortcuts="shortcuts"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
              />
            </div>
          </el-col>
        </el-row>
        <div style="margin-left: 83%; margin-top: 50px">
          <el-button
            type="primary"
            style="width: 92px; height: 40px; border-radius: 20px; /* color: #fff; */"
            @click="search"
            >搜索</el-button
          >
          <el-button
            style="width: 92px; height: 40px; border-radius: 20px; background-color: #eaeaea"
            @click="czbtn"
            >重置</el-button
          >
        </div>
      </el-form>
      <el-form style="position: relative">
        <div style="height: 83px; background-color: #f9f9f9">
          <div style="position: absolute; right: 56px; top: 22px">
            <el-button style="background-color: #ec3a2c; color: #fff" @click="dialogVisible = true"
              >新增</el-button
            >
            <el-button style="background-color: #eaeaea">导入</el-button>
            <el-button style="background-color: #eaeaea">导出</el-button>
          </div>
        </div>
      </el-form>
      <el-form style="background-color: #fff">
        <el-table
          ref="multipleTableRef"
          :data="list"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="用户编号" width="120" type="index" />
          <el-table-column label="用户名称" width="120" prop="name" />
          <el-table-column label="账号" prop="username" />
          <el-table-column label="部门" prop="department">
            <template v-slot="{ row }">
              <span v-if="row.department === 0">财务部</span>
              <span v-if="row.department === 1">营销中心</span>
              <span v-if="row.department === 2">总经办</span>
              <span v-if="row.department === 3">商务中心</span>
            </template>
          </el-table-column>
          <el-table-column label="手机号码" prop="phone" />
          <el-table-column label="状态" prop="status">
            <template v-slot="{ row }">
              <span v-if="row.status === 0">正常</span>
              <span v-else>停用</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="creatTime" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button
                type="text"
                size="small"
                style="width: 26px; height: 17px"
                @click="bj(row.id)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                style="width: 26px; height: 17px; color: #ec3a2c"
                @click="dellist(row.id)"
                >删除</el-button
              >
              <el-button
                type="text"
                size="small"
                style="width: 26px; height: 17px"
                @click="reset(row.id)"
                >重置</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="demo-pagination-block" style="margin-top: 20px; margin-left: 30%">
          <el-pagination
            v-model:current-page="form.page"
            v-model:page-size="form.pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :small="small"
            :disabled="disabled"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-form>
    </div>

    <!-- 新增弹出框 -->
    <el-dialog v-model="dialogVisible" title="添加用户" width="618" :before-close="handleClose">
      <!-- <el-divider /> -->
      <el-form style="margin-left: 80px" label-position="right">
        <el-form-item label="手机号">
          <el-input
            style="width: 260px; height: 40px; margin-left: 15px"
            placeholder="请输入"
            v-model="list2.phone"
          ></el-input>
        </el-form-item>

        <el-form-item label="默认密码">
          <el-input
            style="width: 260px; height: 40px"
            placeholder="请输入"
            v-model="list2.password"
          ></el-input>
        </el-form-item>

        <el-form-item label="真实姓名">
          <el-input
            style="width: 260px; height: 40px"
            placeholder="请输入"
            v-model="list2.name"
          ></el-input>
        </el-form-item>

        <el-form-item label="账号">
          <el-input
            style="width: 260px; height: 40px; margin-left: 28px"
            placeholder="请输入"
            v-model="list2.username"
          ></el-input>
        </el-form-item>

        <el-form-item label="组织结构">
          <el-select
            v-model="list2.department"
            class="m-2"
            placeholder="请选择"
            size="large"
            style="width: 260px"
          >
            <el-option label="财务部" value="0"></el-option>
            <el-option label="营销中心" value="1"></el-option>
            <el-option label="总经办" value="2"></el-option>
            <el-option label="商务中心" value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="职级">
          <el-select
            v-model="value"
            class="m-2"
            placeholder="请选择"
            size="large"
            style="width: 260px; margin-left: 28px"
          >
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="角色分配">
          <div>
            <el-radio-group v-model="list2.roleAssignment" class="ml-4">
              <el-radio label="1" size="large">超级管理员</el-radio>
              <el-radio label="2" size="large">销售主管</el-radio>
              <el-radio label="3" size="large">销售专员</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addlist">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog v-model="dialogVisible2" title="编辑用户" width="618" :before-close="handleClose">
      <!-- <el-divider /> -->
      <el-form style="margin-left: 80px" label-position="right">
        <el-form-item label="手机号">
          <el-input
            style="width: 260px; height: 40px; margin-left: 15px"
            placeholder="请输入"
            v-model="list2.phone"
          ></el-input>
        </el-form-item>

        <el-form-item label="默认密码">
          <el-input
            style="width: 260px; height: 40px"
            placeholder="请输入"
            v-model="list2.password"
          ></el-input>
        </el-form-item>

        <el-form-item label="真实姓名">
          <el-input
            style="width: 260px; height: 40px"
            placeholder="请输入"
            v-model="list2.name"
          ></el-input>
        </el-form-item>

        <el-form-item label="账号">
          <el-input
            style="width: 260px; height: 40px; margin-left: 28px"
            placeholder="请输入"
            v-model="list2.username"
          ></el-input>
        </el-form-item>

        <el-form-item label="组织结构">
          <el-select
            v-model="list2.department"
            class="m-2"
            placeholder="请选择"
            size="large"
            style="width: 260px"
          >
            <el-option label="财务部" value="0"></el-option>
            <el-option label="营销中心" value="1"></el-option>
            <el-option label="总经办" value="2"></el-option>
            <el-option label="商务中心" value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="职级">
          <el-select
            v-model="value"
            class="m-2"
            placeholder="请选择"
            size="large"
            style="width: 260px; margin-left: 28px"
          >
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="角色分配">
          <div>
            <el-radio-group v-model="list2.roleAssignment" class="ml-4">
              <el-radio label="1" size="large">超级管理员</el-radio>
              <el-radio label="2" size="large">销售主管</el-radio>
              <el-radio label="3" size="large">销售专员</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible2 = false">取消</el-button>
          <el-button type="primary" @click="bj2(id)">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 重置密码弹框 -->
    <el-dialog v-model="show" title="提示" width="500" :before-close="handleClose">
      <p style="font-size: 16px">请输入新密码</p>
      <el-input style="width: 300px" v-model="list2.password"></el-input>
      <div style="margin-top: 20px; margin-left: 250px">
        <el-button style="background-color: #eaeaea" @click="show = false">取消</el-button>
        <el-button style="background-color: #00bf7e; color: #fff" @click="qd">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {
  getUserlist,
  delUserlist,
  addUserlist,
  updaUserlist,
  updateuser,
} from '../../../../api/menu'
import { ElMessage } from 'element-plus'
const total = ref(null)
const form = ref({
  page: '1',
  pageSize: '10'
})
const list = ref([])
const dialogVisible = ref(false) //新增弹框
const dialogVisible2 = ref(false) //编辑弹框
const show = ref(false)
const list2 = ref({
  phone: null,
  password: null,
  name: null,
  roleAssignment: null,
  username: null,
  department: null
})
// 获取列表数据
const getUserListAPI = async () => {
  const { data } = await getUserlist(form.value)
  list.value = data.records
  console.log(data)
  total.value = data.total
  // 当前时间
  list.value.forEach((item) => {
    const date = new Date(item.creatTime)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    item.creatTime = `${year}-${'0' + month}-${day}`
    const date1 = new Date(item.endTime)
    const year1 = date1.getFullYear()
    const month1 = date1.getMonth() + 1
    const day1 = date1.getDate()
    item.endTime = `${year1}-${'0' + month1}-${day1}`
  })
}
// 删除用户
const dellist = async (id) => {
  alert('确定删除吗？')
  await delUserlist(id)
  ElMessage.success('删除成功')
  getUserListAPI()
}
// 搜索用户
const search = () => {
  console.log(123)
  getUserListAPI()
}
// 重置列表
const czbtn = () => {
  form.value = {
    page: 1,
    pageSize: 10
  }
  getUserListAPI()
}
// 新增用户
const addlist = async () => {
  await addUserlist(list2.value)
  getUserListAPI()
}
// 编辑用户
const bj = async (id) => {
  dialogVisible2.value = true
  const { data } = await updateuser(id)
  list2.value = data
  console.log(list2)
}
const bj2 = async () => {
  await updaUserlist(list2.value)
  dialogVisible2.value = false
  ElMessage.success('修改成功')
  getUserListAPI()
}
// 重置密码
const reset = (id) => {
  show.value = true
  console.log(id);
}
const qd = () => {
  show.value = false
  ElMessage.success('修改成功')

}
// 分页
const handleCurrentChange = (newpage) => {
  form.value.page = newpage
  //  form.value.pageSize = pageSize
  getUserListAPI(form.value)
}
onMounted(() => {
  getUserListAPI()
})
</script>

<style lang="scss" scoped>
p {
  font-size: 12px;
}
.el-button {
  width: 92px;
  height: 40px;
  border-radius: 20px;
}
:deep(.el-input__wrapper) {
  border-radius: 7px;
  background-color: #f9f9f9;
}
</style>
